package cn.school.async;

import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.school.common.message.WechatMessageSendUtil;
import cn.school.common.utils.HttpClientUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.notice.dao.SchoolNoticeUserDao;
import cn.school.notice.entity.SchoolNotice;
import cn.school.notice.entity.SchoolNoticeUser;
import cn.school.student.entity.StudentInfo;
import cn.school.sys.entity.SysUser;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@EnableAsync
@Configuration
public class AsyncConfig {
    @Resource
    private SchoolNoticeUserDao schoolNoticeUserDao;

    //学校消息通知模板
    private  String noticeTempId;

    @Resource
    private WechatMessageSendUtil wechatMessageSendUtil;

    @Async
    public void sendSchoolNoticeToUser(List<SysUser> userNameList, Long id, SchoolNotice schoolNotice){
        List<String> regIdList = new ArrayList<>();
        String typeName = "0".equals(schoolNotice.getNoticeType())?"学工通知":"安全通知";
        for(SysUser user : userNameList){
            if (Objects.isNull(user.getRegistrationId())) {
                continue;
            }
            SchoolNoticeUser noticeUser = new SchoolNoticeUser();
            noticeUser.setNoticeId(id + "");
            noticeUser.setRecipient(user.getUsername());
            noticeUser.setRecipientType("2");  //老师
            noticeUser.setStatus("0");
            System.out.println(SysUserUtils.getSysUser().getTenantId());
            noticeUser.setTenantId(user.getTenantId());
            schoolNoticeUserDao.insert(noticeUser);
            regIdList.add(user.getRegistrationId());
        }
        //发送极光推送
        sendJGMessage(typeName,schoolNotice.getTitle(),regIdList);
    }

    @Async
    public void sendSchoolNoticeToStudent(List<StudentInfo> studentList, SchoolNotice schoolNotice, Long classId ){
        SchoolNoticeUser noticeUser = null;
        List<WxMaTemplateData> templateDataList = new ArrayList<>();
        //2,设置模版信息（keyword1：类型，keyword2：内容）
        String typeName = "0".equals(schoolNotice.getNoticeType())?"学工通知":"安全通知";
//        WxMaTemplateData data1 = new WxMaTemplateData("thing2", schoolNotice.getTitle());
//        WxMaTemplateData data2 = new WxMaTemplateData("thing4", typeName);
//        WxMaTemplateData data3 = new WxMaTemplateData("time3", DateUtil.today());
//
//        templateDataList.add(data1);
//        templateDataList.add(data2);
//        templateDataList.add(data3);
//        String page ="/pages/home/notice/schoolNoticeDetail/schoolNoticeDetail?id="+schoolNotice.getId();
        List<String> regIdList = new ArrayList<>();
        for(StudentInfo studentInfo : studentList){
            if (Objects.isNull(studentInfo.getStRegistrationId())) {
                continue;
            }
            noticeUser = new SchoolNoticeUser();
            noticeUser.setNoticeId(schoolNotice.getId() + "");
            noticeUser.setRecipient(studentInfo.getStudentId());
            noticeUser.setRecipientType("1"); //学生
            noticeUser.setStatus("0");
            noticeUser.setClassId(classId + "");
            noticeUser.setTenantId(studentInfo.getTenantId());
            schoolNoticeUserDao.insert(noticeUser);
//            sendWxMessage(studentInfo.getStudentOpenid(),noticeTempId,templateDataList,page);
            regIdList.add(studentInfo.getStRegistrationId());
        }

        sendJGMessage(typeName,schoolNotice.getTitle(),regIdList);
    }

    @Async
    public void sendWxMessage(String openId, String tempId, List<WxMaTemplateData> templateDataList,String page){

        wechatMessageSendUtil.sendWxNotice(openId,tempId,templateDataList,page);
    }

    private static String jgMessageUrl = "http://localhost:8098/ny_mall_push/send/pushMore";

    @Async
    public void sendJGMessage(String title, String content,List<String> registIdList){
        List<Map<String,String>> mapList = new ArrayList<>();
        Map<String,String> map = null;
        for(String str:registIdList){
            map = new HashMap<>();
            map.put("title",title);
            map.put("content",content);
            map.put("registrationId",str);
            mapList.add(map);
        }

        HttpClientUtil.httpBodyPost(jgMessageUrl,JSON.toJSONString(mapList),HttpClientUtil.createHeads());
    }
    
    /**
     * 发送家长通知
     *
     * @author alex
     * @param: noticeList
     * @date 2023/3/16 8:30
     * @return
     */
    @Async
    public void sendFamilyNotice(List<Map<String, String>> noticeList) {
        if (CollectionUtils.isEmpty(noticeList)) {
            return;
        }
        HttpClientUtil.httpBodyPost(jgMessageUrl, JSON.toJSONString(noticeList), HttpClientUtil.createHeads());
    }
}
